import { setHeaders, get, post, put, del } from './http.js';

$(document).ready(function () {
    // 初始化轮播图
    initBanner();

    // 初始化商品列表
    post('/api/sku/search/list', { "keyword": "" })
        .then(data => {
            initProductList(data.dataList);
        })
        .catch(error => {
            alert("出错了")
        });


    // 更新购物车数量的函数
    function updateCartCount(count) {
        const $cartCount = $('.cart-count');
        if (count > 0) {
            $cartCount.text(count).show();
        } else {
            $cartCount.text('0').hide();
        }
    }

    // 示例：设置购物车数量
    updateCartCount(8); // 这里的数字应该从后端获取
});

// 初始化轮播图
function initBanner() {
    const $bannerList = $('.banner-list');
    const $bannerDots = $('.banner-dots');
    let currentIndex = 0;

    // 渲染轮播图和指示点
    bannerData.forEach((item, index) => {
        // 添加轮播图
        $bannerList.append(`
            <div class="banner-item ${index === 0 ? 'active' : ''}" data-index="${index}">
                <a href="${item.link}">
                    <img src="${item.image}" alt="${item.title}">
                </a>
            </div>
        `);

        // 添加指示点
        $bannerDots.append(`
            <span class="dot ${index === 0 ? 'active' : ''}" data-index="${index}"></span>
        `);
    });

    // 自动轮播
    let timer = setInterval(nextSlide, 3000);

    // 点击指示点切换
    $('.dot').click(function () {
        clearInterval(timer);
        currentIndex = $(this).data('index');
        showSlide(currentIndex);
        timer = setInterval(nextSlide, 3000);
    });

    // 点击箭头切换
    $('.arrow-left').click(function () {
        clearInterval(timer);
        prevSlide();
        timer = setInterval(nextSlide, 3000);
    });

    $('.arrow-right').click(function () {
        clearInterval(timer);
        nextSlide();
        timer = setInterval(nextSlide, 3000);
    });

    // 鼠标悬停暂停轮播
    $('.banner-container').hover(
        function () {
            clearInterval(timer);
        },
        function () {
            timer = setInterval(nextSlide, 3000);
        }
    );

    // 显示指定索引的轮播图
    function showSlide(index) {
        $('.banner-item').removeClass('active');
        $('.dot').removeClass('active');
        $(`.banner-item[data-index="${index}"]`).addClass('active');
        $(`.dot[data-index="${index}"]`).addClass('active');
    }

    // 下一张
    function nextSlide() {
        currentIndex = (currentIndex + 1) % bannerData.length;
        showSlide(currentIndex);
    }

    // 上一张
    function prevSlide() {
        currentIndex = (currentIndex - 1 + bannerData.length) % bannerData.length;
        showSlide(currentIndex);
    }
}

// 初始化商品列表
function initProductList(productData) {
    const $productList = $('.product-list');

    productData.forEach(product => {
        const $productItem = $(`
            <div class="product-item" data-id="${product.id}">
                <div class="product-img">
                    <img src="${product.image}" alt="${product.title}">
                </div>
                <div class="product-info">
                    <div class="product-title">${product.title}</div>
                    <div class="product-price">
                        <span class="symbol">¥</span>
                        ${product.price.toFixed(2)}
                        <span class="original-price">¥${product.originalPrice.toFixed(2)}</span>
                    </div>
                    <div class="product-bottom">
                        <div class="product-shop">${product.shop}</div>
                        <div class="product-sales">已售${product.sales}件</div>
                    </div>
                </div>
            </div>
        `);

        // 添加点击事件
        $productItem.click(function () {
            // 跳转到商品详情页
            window.location.href = `product-detail.html?id=${product.id}`;
        });

        $productList.append($productItem);
    });
} 